<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Shop extends CI_Controller
{

	private $shop_id;
	private $post;

	public function __construct()
	{
		parent::__construct();
		$this->load->database();
		$this->load->helper('json');
		$this->load->model('order_model');
		$this->load->model('shop_model');
		//验证sessionKey
		$this->init();
	}

	//签名验证 @1
	public function init()
	{

		$post = $this->post = $this->input->post();
		$post['session_key'] = isset($post['session_key']) ? trim($post['session_key']) : '/kbI8BDC75Bc56FZ5F6rtA==';
		if ($post['session_key'] && $session_key = $post['session_key']) {
			if ($userInfo = $this->db->query("select * from app_user where session_key = '$session_key'")->row_array()) {
				if ($tel = $userInfo['tel']) {
					if ($shopTel = $this->db->query("select * from app_shop_tel where tel = '$tel'")->row_array()) {
						$this->shop_id = $shopTel['shop_id'];
					} else {
						send_json(array('code' => 4002, 'data' => '', 'msg' => "所登陆账号,没有商户权限"));
					}
				} else {
					send_json(array('code' => 4002, 'data' => '', 'msg' => "已注销,请重新登陆"));
				}
			} else {
				send_json(array('code' => 4002, 'data' => '', 'msg' => "验签失败，请重新授权登录"));
			}
		} else {
			send_json(array('code' => 4001, 'data' => '', 'msg' => "参数错误"));
		}
	}

	//检查登陆用户是否拥有接口权限
	// private function check_right($user_id,$shop_id)
	// {
	//   $this->db->query("select count(*) as num from `app_shop_user` where `user_id` = '$user_id' and  `shop_id` = '$shop_id' ")->row()['num'];
	//   return $num == 0 ? FALSE : TRUE;
	// }

	//验证商户名是否重复
	// private function name_check($str)
	// {
	//   if(!$str)return FALSE;
	//   $num = $this->db->query("select count(*) as num from `app_shop` where `name` = '$str' ")->row()['num'];
	//   return $num == 0 ? TRUE : FALSE;
	// }

	//创建商户
	// public function createShop()
	// {
	//
	//   $this->load->helper('string');
	//   $this->load->library('form_validation');
	//
	//   $post = $this->input->post();
	//   $post['shop_bn'] = random_string('alnum', 16);//商户编号
	//   $post['created_at'] = $post['updated_at'] = date('Y-m-d H:i:s',time());//创建时间
	//
	//   // $this->form_validation->set_rules('name', 'Name', 'callback_name_check');//验证名称是否重复
	//   // $this->form_validation->set_rules('address', 'Address', 'required');//地址
	//   // $this->form_validation->set_rules('new_user_rate', 'new_user_rate', 'required|regex_match[/^(100|[1-9]?\d(\.\d\d?\d?)?)%$|0$/]');//新用户佣金比例
	//   // $this->form_validation->set_rules('sale_rate', 'sale_rate', 'required|regex_match[/^(100|[1-9]?\d(\.\d\d?\d?)?)%$|0$/]');//销售佣金比例
	//   // $this->form_validation->set_rules('latitude', 'latitude', 'required');//维度
	//   // $this->form_validation->set_rules('longitude', 'longitude', 'required');//经度
	//
	//   // if ($this->form_validation->run() == FALSE) return false;
	//   $res = $this->db->insert('app_shop',$post);
	// }

	//@1
	/**
	 * @SWG\Post(
	 *     summary="商户端首页",
	 *     description="返回商户端首页信息",
	 *     path="/shop/shouYe",
	 *     tags={"商户端"},
	 *     operationId="shouYe",
	 *     produces={"application/json"},
	 *     @SWG\Response(
	 *         response=200,
	 *         description="成功",
	 *     ),
	 *     @SWG\Response(
	 *         response="401",
	 *         description="参数不全",
	 *     ),
	 *     @SWG\Parameter(
	 *         description="签名",
	 *         in="formData",
	 *         name="session_key",
	 *         required=true,
	 *         type="string",
	 *     )
	 * )
	 */
	public function shouYe()
	{

		$post = $this->post;
		$shop_id = $this->shop_id;
		$data = $ret = array();
		//今日总佣金
		$data['todayProperty'] = $this->shop_model->getTodayProperty($shop_id);
		//可提现佣金
		$data['normal'] = $this->shop_model->getNormal($shop_id);
		//今日新客户
		$data['todayNewUser'] = $this->shop_model->getTodayNewUser($shop_id);
		//总客户
		$data['allUser'] = $this->shop_model->getAllUser($shop_id);
		//退款申请数量
		$data['refund'] = $this->shop_model->getRefund($shop_id);

		$ret['code'] = 2000;
		$ret['msg'] = '';
		$ret['data'] = $data;

		send_json($ret);
	}

	//获取佣金（资金） 接受参数shop_id ,type(1,2) @1

	/**
	 * @SWG\Post(
	 *     summary="获取佣金",
	 *     description="获取佣金（资金） 接受参数shop_id ,type(1,2)",
	 *     path="/shop/getProperty",
	 *     tags={"商户端"},
	 *     operationId="shouYe",
	 *     produces={"application/json"},
	 *     @SWG\Response(
	 *         response=200,
	 *         description="成功",
	 *     ),
	 *     @SWG\Response(
	 *         response="401",
	 *         description="参数不全",
	 *     ),
	 *     @SWG\Parameter(
	 *         description="新用户佣金还是销售佣金",
	 *         in="formData",
	 *         name="type",
	 *         required=true,
	 *         type="integer",
	 *     ),
	 *     @SWG\Parameter(
	 *         description="签名",
	 *         in="formData",
	 *         name="session_key",
	 *         required=true,
	 *         type="string",
	 *     )
	 * )
	 */
	public function getProperty()
	{

		$post = $this->post;
		$shop_id = $this->shop_id;
		$ret = array();

		$ret['data'] = $this->shop_model->getProperty($shop_id, $post['type']);
		$ret['code'] = 2000;
		$ret['msg'] = '';

		send_json($ret);
	}

	//获取退款列表 @1

	/**
	 * @SWG\Post(
	 *     summary="获取退款列表",
	 *     path="/shop/reFundList",
	 *     tags={"商户端"},
	 *     operationId="shouYe",
	 *     produces={"application/json"},
	 *     @SWG\Response(
	 *         response=200,
	 *         description="成功",
	 *     ),
	 *     @SWG\Response(
	 *         response="401",
	 *         description="参数不全",
	 *     ),
	 *     @SWG\Parameter(
	 *         description="签名",
	 *         in="formData",
	 *         name="session_key",
	 *         required=true,
	 *         type="string",
	 *     )
	 * )
	 */
	public function reFundList()
	{

		$post = $this->post;
		$shop_id = $this->shop_id;
		$ret = $data = array();

		$data['reFundList'] = $this->shop_model->getReFundList($shop_id);

		$ret['data'] = $data;
		$ret['code'] = 2000;
		$ret['msg'] = '';

		send_json($ret);
	}

	//同意或拒绝申请退款 需要订单id（orderId） 以及 类型type（1,2）1为同意，2为拒绝（拒绝另需要一个reason参数） @1

	/**
	 * @SWG\Post(
	 *     summary="同意或拒绝申请退款",
	 *     description="同意或拒绝申请退款 需要订单id（orderId） 以及 类型type（1,2）1为同意，2为拒绝（拒绝另需要一个reason参数）",
	 *     path="/shop/reFundChangeState",
	 *     tags={"商户端"},
	 *     operationId="reFundChangeState",
	 *     produces={"application/json"},
	 *     @SWG\Response(
	 *         response=200,
	 *         description="成功",
	 *     ),
	 *     @SWG\Response(
	 *         response="401",
	 *         description="参数不全",
	 *     ),
	 *     @SWG\Parameter(
	 *         description="订单id",
	 *         in="formData",
	 *         name="orderId",
	 *         required=true,
	 *         type="string",
	 *     ),
	 *     @SWG\Parameter(
	 *         description="同意1，拒绝2",
	 *         in="formData",
	 *         name="type",
	 *         required=true,
	 *         type="integer",
	 *     ),
	 *     @SWG\Parameter(
	 *         description="拒绝理由",
	 *         in="formData",
	 *         name="reason",
	 *         type="string",
	 *     ),
	 *     @SWG\Parameter(
	 *         description="签名",
	 *         in="formData",
	 *         name="session_key",
	 *         required=true,
	 *         type="string",
	 *     )
	 * )
	 */
	public function reFundChangeState()
	{

		$post = $this->post;
		$shop_id = $this->shop_id;
		$orderId = isset($post['orderId']) ? $post['orderId'] : '';
		$type = isset($post['type']) ? $post['type'] : '';

		$this->requireRes(array($orderId, $type), '');
		$ret = $data = array();

		$this->requireRes($this->db->query("select * from app_order where id = '$orderId' and refund_status = 0")->row()->id, '请求失败,找不到这条退款信息');

		switch ($type) {
			case '1': //同意
				//更新订单表的退款信息
				$this->db->update('app_order', array('refund_status' => 1, 'status' => 2, 'refunded_at' => date('Y-m-d H:i:s', time())), "id = '$orderId'");
				//更新退款表的信息
				$this->db->update('app_refund', array('status' => 1, 'updated_at' => date('Y-m-d H:i:s', time())), "order_id = '$orderId'");

				//推掉这一单新用户的佣金以及销售佣金
				$details = $this->db->query("select * from app_property_detail where detail_id = '$orderId' and source =1")->result_array();

				foreach ($details as $detail) {
					//如果是销售佣金
					// 更新商品信息
					$orderDetail = $this->db->query("select * from app_order_offline_detail where order_id = '$orderId' ")->result_array();
					foreach ($orderDetail as $v) {
						$goodsId = $v['goods_id'];
						$num = $v['num'];
						$this->db->query("update app_goods set num = num + '$num' where id = '$goodsId' ");
						$this->db->query("update app_shop_goods set num = num + '$num' where goods_id = '$goodsId' and shop_id = '$shop_id'");
					}

					$money = $detail['money']; //佣金金额
					$shop_id = $detail['shop_id']; //佣金店铺id
					unset($detail['id']);
					$detail['source'] = 4; //设置为退款
					$detail['type'] = 2; //设置为支出
					$detail['created_at'] = date('Y-m-d H:i:s', time());
					//  添加佣金detail表信息
					$this->db->insert('app_property_detail', $detail);
					//  更新佣金总表信息
					$this->db->query("update app_property set total = total - '$money' , normal = normal -'$money' where shop_id = '$shop_id' ");
				}

				//调用微信退款的接口
				$thisorder = $this->db->query("select * from app_order where id = '$orderId'")->row_array();
				$thisrefund = $this->db->query("select * from app_refund where order_id = '$orderId'")->row_array();
				require dirname(__FILE__) . "/Wxrefund.php";
				$Wxrefund = new Wxrefund();
				$param = array();
				$param['refund_bn'] = $thisrefund['refund_bn'];
				$param['transaction_id'] = $thisorder['transaction_id'];
				$param['total_fee'] = $param['refund_fee'] = $thisorder['money'];
				$Wxrefund->index($param);
				break;

			default: //拒绝
				//更新订单表的退款信息
				$this->db->update('app_order', array('refund_status' => 2, 'completed_at' => date('Y-m-d H:i:s', time())), "id = '$orderId'");
				//更新退款表的信息
				$this->db->update('app_refund', array('status' => 2, 'updated_at' => date('Y-m-d H:i:s', time()), 'reason' => $post['reason']), "order_id = '$orderId'");
				$ret['code'] = 2000;
				$ret['msg'] = '';

				send_json($ret);
				break;
		}
	}

	// //根据订单去退款（线上需要，线下先不做）
	// private function refund($orderId)
	// {
	//
	// }

	//@1
	/**
	 * @SWG\Post(
	 *     summary="获取本店所有商品价格",
	 *     path="/shop/getGoodsPrice",
	 *     tags={"商户端"},
	 *     operationId="getGoodsPrice",
	 *     produces={"application/json"},
	 *     @SWG\Response(
	 *         response=200,
	 *         description="成功",
	 *     ),
	 *     @SWG\Response(
	 *         response="401",
	 *         description="参数不全",
	 *     ),
	 *     @SWG\Parameter(
	 *         description="签名",
	 *         in="formData",
	 *         name="session_key",
	 *         required=true,
	 *         type="string",
	 *     )
	 * )
	 */
	//商品价格接口
	public function getGoodsPrice()
	{

		$post = $this->post;
		$shop_id = $this->shop_id;
		$ret = $data = array();

		$data = $this->db->query("
                select
                  b.title as name ,b.goods_bn as goodsbn ,b.price as price, b.pic_4 as pic ,c.created_at as created_at ,c.updated_at as updated_at
                from
                  app_shop_goods a
                left join
                  app_goods b
                on
                  a.goods_id = b.id
                left join
                  app_restock c
                on
                  a.restock_id = c.id
                where
                  a.shop_id = '$shop_id' and b.status = 1
              ")->result_array();

		$ret['code'] = 2000;
		$ret['msg'] = '';
		$ret['data'] = $data;
		send_json($ret);
	}

	//商品价格搜索 （编号。名称） 传递搜索关键词 keyword @1

	/**
	 * @SWG\Post(
	 *     summary="商品价格列表搜索",
	 *     path="/shop/searchGoodsPrice",
	 *     tags={"商户端"},
	 *     operationId="shouYe",
	 *     produces={"application/json"},
	 *     @SWG\Response(
	 *         response=200,
	 *         description="成功",
	 *     ),
	 *     @SWG\Response(
	 *         response="401",
	 *         description="参数不全",
	 *     ),
	 *     @SWG\Parameter(
	 *         description="搜索关键词",
	 *         in="formData",
	 *         name="keyword",
	 *         required=true,
	 *         type="string",
	 *     ),
	 *     @SWG\Parameter(
	 *         description="签名",
	 *         in="formData",
	 *         name="session_key",
	 *         required=true,
	 *         type="string",
	 *     )
	 * )
	 */
	public function searchGoodsPrice()
	{

		$post = $this->post;
		$shop_id = $this->shop_id;
		$keyword = isset($post['keyword']) ? trim($post['keyword']) : '';
		$sqlLimit = '';
		$keyword && $sqlLimit = " and (b.title like '%{$keyword}%' or b.goods_bn like '%{$keyword}%' )";
		$ret = $data = array();

		$data = $this->db->query("
                select
                  b.title as name ,b.goods_bn as goodsbn ,b.price as price, b.pic_4 as pic ,c.created_at as created_at ,c.updated_at as updated_at
                from
                  app_shop_goods a
                left join
                  app_goods b
                on
                  a.goods_id = b.id
                left join
                  app_restock c
                on
                  a.restock_id = c.id
                where
                  a.shop_id = '$shop_id' and b.status = 1  " . $sqlLimit . "
              ")->result_array();

		$ret['code'] = 2000;
		$ret['msg'] = '';
		$ret['data'] = $data;
		send_json($ret);
	}

	//客户管理 传入参数，今天或者全部type（1,2）@1

	/**
	 * @SWG\Post(
	 *     summary="客户管理",
	 *     path="/shop/custManage",
	 *     tags={"商户端"},
	 *     operationId="custManage",
	 *     produces={"application/json"},
	 *     @SWG\Response(
	 *         response=200,
	 *         description="成功",
	 *     ),
	 *     @SWG\Response(
	 *         response="401",
	 *         description="参数不全",
	 *     ),
	 *     @SWG\Parameter(
	 *         description="今天的用户1，全部用户2",
	 *         in="formData",
	 *         name="type",
	 *         required=true,
	 *         type="integer",
	 *     ),
	 *     @SWG\Parameter(
	 *         description="签名",
	 *         in="formData",
	 *         name="session_key",
	 *         required=true,
	 *         type="string",
	 *     )
	 * )
	 */
	public function custManage()
	{

		$post = $this->post;
		$shop_id = $this->shop_id;
		$type = isset($post['type']) ? $post['type'] : '';
		$this->requireRes($type, '');

		$sqlLimit = $type == '1' ? " a.id in (select user_id from app_order where shop_id = '$shop_id' and to_days(created_at) = to_days(now()) group by user_id)" : "1";

		$data = $this->db->query("
        select
          a.id as userId, a.nick_name as name, h.orderNum as orderNum, g.thisOrderNum as thisOrderNum, a.created_at as registerTime, c.name as shopName, f.lastOrderTime as lastOrderTime, f.totMoney as totMoney
        from
          (select * from app_user where id in (select user_id from app_order where status=1)) a
        inner join
          app_shop_user b
        on
          a.id = b.user_id
        inner join
          app_shop c
        on
          b.shop_id = c.id
        left join
          (
            select
              e.user_id as id, SUM(d.money) as totMoney, MAX(e.created_at) as lastOrderTime
            from
              app_property_detail d
            left join
              app_order e
            on
              d.detail_id = e.id
            where
              e.status =1 and d.shop_id = '$shop_id' and d.source in (1,2)
            group by e.user_id
          ) f
        on
          a.id = f.id
        left join
          (
            select
              user_id as userid,count(*) as thisOrderNum
            from app_order
            where
              shop_id = '$shop_id' and status =1
            group by
              user_id
          ) as g
        on
          a.id = g.userid
        left join
          (
            select
              user_id as userid,count(*) as OrderNum
            from app_order
            where
              status =1
            group by
              user_id
          ) as h
        on
          a.id = h.userid
        where
          " . $sqlLimit . "
      ")->result_array();

		$ret['code'] = 2000;
		$ret['msg'] = '';
		$ret['data'] = $data;
		send_json($ret);
	}

	//搜索客户 传入参数，今天或者全部type（1,2），搜索关键词keyword 暂定只搜索昵称@1

	/**
	 * @SWG\Post(
	 *     summary="搜索客户",
	 *     path="/shop/searchCustManage",
	 *     tags={"商户端"},
	 *     operationId="searchCustManage",
	 *     produces={"application/json"},
	 *     @SWG\Response(
	 *         response=200,
	 *         description="成功",
	 *     ),
	 *     @SWG\Response(
	 *         response="401",
	 *         description="参数不全",
	 *     ),
	 *     @SWG\Parameter(
	 *         description="今天的用户1，全部用户2",
	 *         in="formData",
	 *         name="type",
	 *         required=true,
	 *         type="integer",
	 *     ),
	 *     @SWG\Parameter(
	 *         description="关键词",
	 *         in="formData",
	 *         name="keyword",
	 *         required=true,
	 *         type="string",
	 *     ),
	 *     @SWG\Parameter(
	 *         description="签名",
	 *         in="formData",
	 *         name="session_key",
	 *         required=true,
	 *         type="string",
	 *     )
	 * )
	 */
	public function searchCustManage()
	{

		$post = $this->post;
		$shop_id = $this->shop_id;
		$type = $post['type']; //1代表搜索今天，2代表搜索全部
		$keyword = isset($post['keyword']) ? trim($post['keyword']) : ''; //搜素关键词
		$keyword ? $keyWordSql = " and a.nick_name like '%$keyword%' " : $keyWordSql = '';

		// 获取今天下单的所有用户
		$sqlLimit = $type == '1' ? " a.id in (select user_id from app_order where shop_id = '$shop_id' and to_days(created_at) = to_days(now()) group by user_id)" : "1";

		$data = $this->db->query("
        select
          a.id as userId, a.nick_name as name, h.orderNum as orderNum, g.thisOrderNum as thisOrderNum, a.created_at as registerTime, c.name as shopName, f.lastOrderTime as lastOrderTime, f.totMoney as totMoney
        from
          (select * from app_user where id in (select user_id from app_order where status=1)) a
        inner join
          app_shop_user b
        on
          a.id = b.user_id
        inner join
          app_shop c
        on
          b.shop_id = c.id
        left join
          (
            select
              e.user_id as id, SUM(d.money) as totMoney, MAX(e.created_at) as lastOrderTime
            from
              app_property_detail d
            left join
              app_order e
            on
              d.detail_id = e.id
            where
              e.status =1 and d.shop_id = '$shop_id' and d.source in (1,2)
            group by e.user_id
          ) f
        on
          a.id = f.id
        left join
          (
            select
              user_id as userid,count(*) as thisOrderNum
            from app_order
            where
              shop_id = '$shop_id' and status =1
            group by
              user_id
          ) as g
        on
          a.id = g.userid
        left join
          (
            select
              user_id as userid,count(*) as OrderNum
            from app_order
            where
              status =1
            group by
              user_id
          ) as h
        on
          a.id = h.userid
        where
          " . $sqlLimit . "$keyWordSql
      ")->result_array();

		$ret['code'] = 2000;
		$ret['msg'] = '';
		$ret['data'] = $data;
		send_json($ret);
	}

	//根据用户id获取用户的每个订单的详细佣金信息@1

	/**
	 * @SWG\Post(
	 *     summary="获取用户订单佣金信息",
	 *     path="/shop/getOrderDetailByUserId",
	 *     tags={"商户端"},
	 *     operationId="getOrderDetailByUserId",
	 *     produces={"application/json"},
	 *     @SWG\Response(
	 *         response=200,
	 *         description="成功",
	 *     ),
	 *     @SWG\Response(
	 *         response="401",
	 *         description="参数不全",
	 *     ),
	 *     @SWG\Parameter(
	 *         description="签名",
	 *         in="formData",
	 *         name="session_key",
	 *         required=true,
	 *         type="string",
	 *     ),
	 *     @SWG\Parameter(
	 *         description="用户id",
	 *         in="formData",
	 *         name="userId",
	 *         required=true,
	 *         type="integer",
	 *     )
	 * )
	 */
	public function getOrderDetailByUserId()
	{
		$post = $this->post;
		$shop_id = $this->shop_id;
		$userId = isset($post['userId']) ? $post['userId'] : '';
		$this->requireRes($userId, '');
		$this->requireRes($this->db->query("select * from app_user where id = '$userId'")->row_array(), '未根据用户id查找到用户');
		//在本店下单的其他店的客户
		$s1 = $this->db->query("
         select
           a.order_bn as orderBn, a.created_at as createdAt, c.name as orderShopName, f.created_at as registerTime, d.name as registerShopName, a.money as orderMoney, g.commission
         from
           app_order a
         left join
           app_user b
         on
           a.user_id = b.id
         left join
           app_shop c
         on
           a.shop_id = c.id
         left join
           app_order_offline g
         on
           a.id = g.order_id
         left join
           app_shop d
         on
           g.parent_shop_id = d.id
         left join
           app_user f
         on
           a.user_id = f.id
         where
           a.user_id = '$userId' and a.shop_id = '$shop_id' and g.parent_shop_id <> '$shop_id'  and a.status =1
       ")->result_array();

		//在本店下单的本店客户
		$s2 = $this->db->query("
         select
           a.order_bn as orderBn, a.created_at as createdAt, c.name as orderShopName, f.created_at as registerTime, d.name as registerShopName, a.money as orderMoney, IFNULL(g.commission,0) + IFNULL(g.parent_shop_commission,0) as commission
         from
           app_order a
         left join
           app_user b
         on
           a.user_id = b.id
         left join
           app_shop c
         on
           a.shop_id = c.id
         left join
           app_order_offline g
         on
           a.id = g.order_id
         left join
           app_shop d
         on
           g.parent_shop_id = d.id
         left join
           app_user f
         on
           a.user_id = f.id
         where
           a.user_id = '$userId' and a.shop_id = '$shop_id' and g.parent_shop_id = '$shop_id' and  a.status =1
       ")->result_array();

		//在其他店下单的本店客户
		$s3 = $this->db->query("
         select
           a.order_bn as orderBn, a.created_at as createdAt, c.name as orderShopName, f.created_at as registerTime, d.name as registerShopName, a.money as orderMoney, g.parent_shop_commission as commission
         from
           app_order a
         left join
           app_user b
         on
           a.user_id = b.id
         left join
           app_shop c
         on
           a.shop_id = c.id
         left join
           app_order_offline g
         on
           a.id = g.order_id
         left join
           app_shop d
         on
           g.parent_shop_id = d.id
         left join
           app_user f
         on
           a.user_id = f.id
         where
           a.user_id = '$userId' and a.shop_id <> '$shop_id' and g.parent_shop_id = '$shop_id'  and a.status =1
       ")->result_array();

		$data = array_merge($s1, $s2, $s3);

		$ret['code'] = 2000;
		$ret['msg'] = '';
		$ret['data'] = $data;
		send_json($ret);
	}

	//获取订单列表 传入商户id shop_id@1

	/**
	 * @SWG\Post(
	 *     summary="订单管理",
	 *     path="/shop/orderManage",
	 *     tags={"商户端"},
	 *     operationId="orderManage",
	 *     produces={"application/json"},
	 *     @SWG\Response(
	 *         response=200,
	 *         description="成功",
	 *     ),
	 *     @SWG\Response(
	 *         response="401",
	 *         description="参数不全",
	 *     ),
	 *     @SWG\Parameter(
	 *         description="签名",
	 *         in="formData",
	 *         name="session_key",
	 *         required=true,
	 *         type="string",
	 *     )
	 * )
	 */
	public function orderManage()
	{

		$post = $this->post;
		$shop_id = $this->shop_id;

		$data = $this->db->query("
                select
                  a.id as orderId ,b.nick_name as name ,a.order_bn as orderNum ,a.money as money,a.created_at as time
                from
                  app_order a
                left join
                  app_user b
                on
                  a.user_id = b.id
                where
                  a.shop_id = '$shop_id' and a.status = 1 and a.type = 2
                order by
                  a.created_at desc
              ")->result_array();

		$ret['code'] = 2000;
		$ret['msg'] = '';
		$ret['data'] = $data;
		send_json($ret);
	}

	/**
	 * @SWG\Post(
	 *     summary="订单列表",
	 *     path="/shop/orderList",
	 *     tags={"商户端2.0"},
	 *     operationId="orderList",
	 *     produces={"application/json"},
	 *     @SWG\Response(
	 *         response=200,
	 *         description="成功",
	 *     ),
	 *     @SWG\Response(
	 *         response="401",
	 *         description="参数不全",
	 *     ),
	 *     @SWG\Parameter(
	 *         description="签名",
	 *         in="formData",
	 *         name="session_key",
	 *         required=true,
	 *         type="string",
	 *     ),
	 *     @SWG\Parameter(
	 *         description="状态",
	 *         in="formData",
	 *         name="status",
	 *         required=true,
	 *         type="string",
	 *     )
	 * )
	 */
	public function orderList()
	{
		$post = $this->post;
		$shop_id = $this->shop_id;
		$type = isset($post['type']) ? $post['type'] : '';
		$status = isset($post['status']) ? $post['status'] : '';
		$start_time = isset($post['start_time']) ? $post['start_time'] : '';
		$end_time = isset($post['end_time']) ? $post['end_time'] : '';
		$keyword = isset($post['keyword']) ? $post['keyword'] : '';
		$keyword && $keyWordSql = " and (a.order_bn like '%$keyword%') ";
		$keyWordSql = isset($keyWordSql) ? $keyWordSql : '';

		if ($status) {
			$where = "a.shop_id = '$shop_id' or c.shop_id = '$shop_id' and a.status = '$status' $keyWordSql ";
		} else {
			$where = "a.shop_id = '$shop_id' or c.shop_id = '$shop_id' and a.status != 0 $keyWordSql";
		}
		if ($start_time && $end_time) {
			$start_time = date('Y-m-d', $start_time);
			$end_time = date('Y-m-d', $end_time);
			$where .= " and a.created_at between $start_time AND $end_time";
		}
		if ($type) {
			$where .= " and a.type = $type";
		}
		$page = isset($post['page']) ? $post['page'] : 1;
		$start = ($page - 1) * 10;

//        echo "select
//                  a.id as orderId ,b.nick_name as name ,a.order_bn as orderNum ,a.money as money,a.created_at as time,a.delivery_method as delivery_method,a.delivered_at as delivered_at,a.delivery_time as delivery_time, a.type as type,  a.status as status
//                from
//                  app_order a
//                left join
//                  app_user b
//                on
//                  a.user_id = b.id
//                left join
//                  app_order_online c
//                on
//                  c.order_id = a.id
//                where
//                  $where
//                order by
//                  a.created_at desc";
		$data = $this->db->query("
                select
                  a.id as orderId ,b.nick_name as name ,a.order_bn as orderNum ,a.money as money,a.created_at as time,a.delivery_method as delivery_method,a.delivered_at as delivered_at,a.delivery_time as delivery_time, a.type as type,  a.status as status
                from
                  app_order a
                left join
                  app_user b
                on
                  a.user_id = b.id
                left join
                  app_order_online c
                on
                  c.order_id = a.id
                where
                  $where
                order by
                  a.created_at desc
			    limit $start,10
              ")->result_array();
		$ret['code'] = 2000;
		$ret['msg'] = '';
		$ret['data'] = $data;
		send_json($ret);
	}

	/**
	 * @SWG\Post(
	 *     summary="搜索订单列表",
	 *     path="/shop/searchOrderList",
	 *     tags={"商户端2.0"},
	 *     operationId="searchOrderLists",
	 *     produces={"application/json"},
	 *     @SWG\Response(
	 *         response=200,
	 *         description="成功",
	 *     ),
	 *     @SWG\Response(
	 *         response="401",
	 *         description="参数不全",
	 *     ),
	 *     @SWG\Parameter(
	 *         description="关键词",
	 *         in="formData",
	 *         name="keyword",
	 *         required=true,
	 *         type="string",
	 *     ),
	 *     @SWG\Parameter(
	 *         description="签名",
	 *         in="formData",
	 *         name="session_key",
	 *         required=true,
	 *         type="string",
	 *     )
	 * )
	 */
	public function searchOrderList()
	{
		$post = $this->post;
		$shop_id = $this->shop_id;
		$keyword = isset($post['keyword']) ? $post['keyword'] : '';
		$keyword && $keyWordSql = " and (a.order_bn like '%$keyword%' or b.nick_name like '%$keyword%') ";
		$keyWordSql = isset($keyWordSql) ? $keyWordSql : '';

		$data = $this->db->query("
                select
                  a.id as orderId ,b.nick_name as name ,a.order_bn as orderNum ,a.created_at as created_at ,a.money as money,a.created_at as time
                from
                  app_order a
                left join
                  app_user b
                on
                  a.user_id = b.id
                where
                  a.`shop_id` = '$shop_id' " . $keyWordSql . "  and a.status != 0
                order by
                  created_at desc
              ")->result_array();

		$ret['code'] = 2000;
		$ret['msg'] = '';
		$ret['data'] = $data;
		send_json($ret);
	}

	//搜索订单列表 传入商户id shop_id 以及搜索的keyword 搜索昵称和订单编号@1

	/**
	 * @SWG\Post(
	 *     summary="搜索订单列表",
	 *     path="/shop/searchOrderManage",
	 *     tags={"商户端"},
	 *     operationId="searchOrderManage",
	 *     produces={"application/json"},
	 *     @SWG\Response(
	 *         response=200,
	 *         description="成功",
	 *     ),
	 *     @SWG\Response(
	 *         response="401",
	 *         description="参数不全",
	 *     ),
	 *     @SWG\Parameter(
	 *         description="关键词",
	 *         in="formData",
	 *         name="keyword",
	 *         required=true,
	 *         type="string",
	 *     ),
	 *     @SWG\Parameter(
	 *         description="签名",
	 *         in="formData",
	 *         name="session_key",
	 *         required=true,
	 *         type="string",
	 *     )
	 * )
	 */
	public function searchOrderManage()
	{

		$post = $this->post;
		$shop_id = $this->shop_id;
		$keyword = isset($post['keyword']) ? $post['keyword'] : '';
		$keyword && $keyWordSql = " and (a.order_bn like '%$keyword%' or b.nick_name like '%$keyword%') ";
		$keyWordSql = isset($keyWordSql) ? $keyWordSql : '';

		$data = $this->db->query("
                select
                  a.id as orderId ,b.nick_name as name ,a.order_bn as orderNum ,a.created_at as created_at ,a.money as money,a.created_at as time
                from
                  app_order a
                left join
                  app_user b
                on
                  a.user_id = b.id
                where
                  a.`shop_id` = '$shop_id' " . $keyWordSql . "  and a.status = 1 and a.type = 2
                order by
                  created_at desc
              ")->result_array();

		$ret['code'] = 2000;
		$ret['msg'] = '';
		$ret['data'] = $data;
		send_json($ret);
	}



	//订单详情 传入商户id ，订单编号 @1

	/**
	 * @SWG\Post(
	 *     summary="订单详情",
	 *     path="/shop/orderDetail",
	 *     tags={"商户端"},
	 *     operationId="orderDetail",
	 *     produces={"application/json"},
	 *     @SWG\Response(
	 *         response=200,
	 *         description="成功",
	 *     ),
	 *     @SWG\Response(
	 *         response="401",
	 *         description="参数不全",
	 *     ),
	 *     @SWG\Parameter(
	 *         description="订单id",
	 *         in="formData",
	 *         name="orderId",
	 *         required=true,
	 *         type="string",
	 *     ),
	 *     @SWG\Parameter(
	 *         description="签名",
	 *         in="formData",
	 *         name="session_key",
	 *         required=true,
	 *         type="string",
	 *     )
	 * )
	 */
	public function orderDetail()
	{

		$post = $this->post;
		$shop_id = $this->shop_id;
		$orderId = isset($post['orderId']) ? trim($post['orderId']) : '';
		$this->requireRes($orderId, '');

		$this->requireRes($this->db->query("select * from app_order where shop_id = '$shop_id' and id = '$orderId'")->row_array(), '未根据订单编号找到订单');
		//订单信息
		$data['info'] = $this->db->query("
              select
                a.order_bn ,a.money as money, a.created_at,c.money as xsCommission, b.money as xyhCommission
              from
                `app_order` a
              left join
                app_property_detail b
              on
                b.detail_id = a.id and b.source = 2 and b.shop_id = '$shop_id'
              left join
                app_property_detail c
              on
                c.detail_id = a.id and c.source = 1 and c.shop_id = '$shop_id'
              where
                a.`id` = '$orderId' and a.`shop_id` = '$shop_id'
             ")->row_array();

		//订单详细信息
		$data['detail'] = $this->db->query("
                          select
                            b.title as goodName ,a.num as num, b.price as price, b.pic_4 as pic, b.goods_bn
                          from
                            app_order_offline_detail a
                          left join
                            app_goods b
                          on
                            a.goods_id = b.id
                          where
                            a.order_id = '$orderId'
                          order by
                            a.id desc
                        ")->result_array();

		$ret['code'] = 2000;
		$ret['msg'] = '';
		$ret['data'] = $data;
		send_json($ret);
	}

	//库存管理界面 @1

	/**
	 * @SWG\Post(
	 *     summary="库存管理界面",
	 *     description="返回获取库存低的商品数量lackGoodsNum,未送达的补货数量unSentRestockNum",
	 *     path="/shop/lackStockManage",
	 *     tags={"商户端"},
	 *     operationId="lackStockManage",
	 *     produces={"application/json"},
	 *     @SWG\Response(
	 *         response=200,
	 *         description="成功",
	 *     ),
	 *     @SWG\Response(
	 *         response="401",
	 *         description="参数不全",
	 *     ),
	 *     @SWG\Parameter(
	 *         description="签名",
	 *         in="formData",
	 *         name="session_key",
	 *         required=true,
	 *         type="string",
	 *     )
	 * )
	 */
	public function lackStockManage()
	{

		$post = $this->post;
		$shop_id = $this->shop_id;
		// 获取库存低的商品数量暂定小于等于三
		$data['lackGoodsNum'] = $this->db->query("select count(*) as lackGoodsNum from app_shop_goods where num < 4 and shop_id = '$shop_id'")->row()->lackGoodsNum;
		// 补货是否有待送达
		$data['unSentRestockNum'] = $this->db->query("select count(*) as unSentRestockNum from app_restock where status = 0 and shop_id = '$shop_id'")->row()->unSentRestockNum;

		$ret['code'] = 2000;
		$ret['msg'] = '';
		$ret['data'] = $data;
		send_json($ret);
	}

	//库存详情 @1

	/**
	 * @SWG\Post(
	 *     summary="库存详情",
	 *     path="/shop/lackManage",
	 *     tags={"商户端"},
	 *     operationId="lackManage",
	 *     produces={"application/json"},
	 *     @SWG\Response(
	 *         response=200,
	 *         description="成功",
	 *     ),
	 *     @SWG\Response(
	 *         response="401",
	 *         description="参数不全",
	 *     ),
	 *     @SWG\Parameter(
	 *         description="签名",
	 *         in="formData",
	 *         name="session_key",
	 *         required=true,
	 *         type="string",
	 *     )
	 * )
	 */
	public function lackManage()
	{

		$post = $this->post;
		$shop_id = $this->shop_id;

		$data = $this->shop_model->getLackManage($shop_id);

		$ret['code'] = 2000;
		$ret['msg'] = '';
		$ret['data'] = $data;
		send_json($ret);
	}

	//库存详情搜索 传入keyword @1

	/**
	 * @SWG\Post(
	 *     summary="库存详情搜索",
	 *     path="/shop/searchLackManage",
	 *     tags={"商户端"},
	 *     operationId="searchLackManage",
	 *     produces={"application/json"},
	 *     @SWG\Response(
	 *         response=200,
	 *         description="成功",
	 *     ),
	 *     @SWG\Response(
	 *         response="401",
	 *         description="参数不全",
	 *     ),
	 *     @SWG\Parameter(
	 *         description="关键词",
	 *         in="formData",
	 *         name="keyword",
	 *         required=true,
	 *         type="string",
	 *     ),
	 *     @SWG\Parameter(
	 *         description="签名",
	 *         in="formData",
	 *         name="session_key",
	 *         required=true,
	 *         type="string",
	 *     )
	 * )
	 */
	public function searchLackManage()
	{

		$post = $this->post;
		$shop_id = $this->shop_id;
		$keyword = $post['keyword'];
		$keyWordSql = " and (c.title like '%$keyword%' or c.goods_bn like '%$keyword%') ";

		$data = $this->db->query("
                select
                  c.title as name, c.goods_bn as goodsBn  , c.pic_4 as pic,a.num as goodsNum, b.name as shopName, f.created_at as fromTime, f.updated_at as endTime
                from
                  app_shop_goods a
                left join
                  app_shop b
                on
                  a.shop_id = b.id
                left join
                  app_goods c
                on
                  a.goods_id = c.id
                left join
                  (select
                    d.goods_id as goodsid ,e.created_at as created_at ,e.updated_at as updated_at
                  from
                    app_restock_detail d
                  left join
                    app_restock e
                  on
                    d.restock_id = e.id
                  where
                    e.shop_id = '$shop_id' and e.status = 1
                  group by
                    d.goods_id
                  order by
                    e.id desc) as f
                on
                  a.goods_id = f.goodsid
                where
                  a.shop_id = '$shop_id'
                  " . $keyWordSql . "
                order by a.id desc
              ")->result_array();

		$ret['code'] = 2000;
		$ret['msg'] = '';
		$ret['data'] = $data;
		send_json($ret);
	}

	//补货界面，获取所有的补货列表 @1

	/**
	 * @SWG\Post(
	 *     summary="补货界面",
	 *     path="/shop/getStockList",
	 *     tags={"商户端"},
	 *     operationId="getStockList",
	 *     produces={"application/json"},
	 *     @SWG\Response(
	 *         response=200,
	 *         description="成功",
	 *     ),
	 *     @SWG\Response(
	 *         response="401",
	 *         description="参数不全",
	 *     ),
	 *     @SWG\Parameter(
	 *         description="签名",
	 *         in="formData",
	 *         name="session_key",
	 *         required=true,
	 *         type="string",
	 *     )
	 * )
	 */
	public function getStockList()
	{

		$post = $this->post;
		$shop_id = $this->shop_id;

		$data = $this->db->query("
                select
                  a.restock_bn as restockBn, a.created_at as fromTime, a.updated_at as endTime, a.status as status, GROUP_CONCAT(b.name) as goodsNames
                from
                  app_restock a
                right join
                  (select c.restock_id as restock_id,d.title as name
                  from
                    app_restock_detail c
                  left join
                    app_goods d
                  on
                    c.goods_id = d.id
                  ) as b
                on
                  a.id = b.restock_id
                where
                  a.`shop_id` = '$shop_id'
                group by
                  a.id
                order by
                  a.status asc , a.id desc
              ")->result_array();

		$ret['code'] = 2000;
		$ret['msg'] = '';
		$ret['data'] = $data;
		send_json($ret);
	}

	//补货界面搜索 keyword @1

	/**
	 * @SWG\Post(
	 *     summary="补货界面搜索",
	 *     path="/shop/searchStockList",
	 *     tags={"商户端"},
	 *     operationId="searchStockList",
	 *     produces={"application/json"},
	 *     @SWG\Response(
	 *         response=200,
	 *         description="成功",
	 *     ),
	 *     @SWG\Response(
	 *         response="401",
	 *         description="参数不全",
	 *     ),
	 *     @SWG\Parameter(
	 *         description="关键词",
	 *         in="formData",
	 *         name="keyword",
	 *         required=true,
	 *         type="string",
	 *     ),
	 *     @SWG\Parameter(
	 *         description="签名",
	 *         in="formData",
	 *         name="session_key",
	 *         required=true,
	 *         type="string",
	 *     )
	 * )
	 */
	public function searchStockList()
	{

		$post = $this->post;
		$shop_id = $this->shop_id;
		$keyword = isset($post['keyword']) ? $post['keyword'] : '';
		$keyWordSql = '';
		$keyword && $keyWordSql = " and (a.restock_bn like '%$keyword%') ";

		$data = $this->db->query("
                select
                  a.restock_bn as restockBn, a.created_at as fromTime, a.updated_at as endTime, a.status as status, GROUP_CONCAT(b.name) as goodsNames
                from
                  app_restock a
                right join
                  (select c.restock_id as restock_id,d.title as name
                  from
                    app_restock_detail c
                  left join
                    app_goods d
                  on
                    c.goods_id = d.id
                  ) as b
                on
                  a.id = b.restock_id
                where
                  a.`shop_id` = '$shop_id'
                  " . $keyWordSql . "
                group by
                  a.id
                order by
                  a.status asc , a.id desc
              ")->result_array();

		$ret['code'] = 2000;
		$ret['msg'] = '';
		$ret['data'] = $data;
		send_json($ret);
	}

	//根据货单号获取货单详情 @1

	/**
	 * @SWG\Post(
	 *     summary="获取货单详情",
	 *     path="/shop/getStockDetail",
	 *     tags={"商户端"},
	 *     operationId="getStockDetail",
	 *     produces={"application/json"},
	 *     @SWG\Response(
	 *         response=200,
	 *         description="成功",
	 *     ),
	 *     @SWG\Response(
	 *         response="401",
	 *         description="参数不全",
	 *     ),
	 *     @SWG\Parameter(
	 *         description="货单号",
	 *         in="formData",
	 *         name="restockBn",
	 *         required=true,
	 *         type="string",
	 *     ),
	 *     @SWG\Parameter(
	 *         description="签名",
	 *         in="formData",
	 *         name="session_key",
	 *         required=true,
	 *         type="string",
	 *     )
	 * )
	 */
	public function getStockDetail()
	{

		$post = $this->post;
		$shop_id = $this->shop_id;
		$restockBn = $post['restockBn'];
		$this->requireRes($restockBn, '');
		//补货信息
		$restock = $this->db->query("select * from app_restock where restock_bn = '$restockBn' and shop_id = '$shop_id'")->row();
		$this->requireRes($restock, '未根据货单号找到信息');
		$restockId = $restock->id;
		//补货详细信息
		$restockDetail = $this->db->query("select b.title as goodsName, a.num as num, b.price as price from app_restock_detail a left join app_goods b on a.goods_id = b.id where a.restock_id = '$restockId' ")->result_array();
		//平台手机号码
		$phone = $this->db->query("select * from app_key_value where k = 'phone'")->row()->v;
		$data['phone'] = $phone;
		$data['restockDetail'] = $restockDetail;
		$data['restockBn'] = $restockBn;
		$data['created_at'] = $restock->created_at;
		$data['updated_at'] = $restock->updated_at;
		$data['status'] = $restock->status; //0未送达，1已送达

		$ret['code'] = 2000;
		$ret['msg'] = '';
		$ret['data'] = $data;
		send_json($ret);
	}

	//确认补货收到  @1

	/**
	 * @SWG\Post(
	 *     summary="确认收到补货",
	 *     path="/shop/confirmStock",
	 *     tags={"商户端"},
	 *     operationId="confirmStock",
	 *     produces={"application/json"},
	 *     @SWG\Response(
	 *         response=200,
	 *         description="成功",
	 *     ),
	 *     @SWG\Response(
	 *         response="401",
	 *         description="参数不全",
	 *     ),
	 *     @SWG\Parameter(
	 *         description="货单号",
	 *         in="formData",
	 *         name="restockBn",
	 *         required=true,
	 *         type="string",
	 *     ),
	 *     @SWG\Parameter(
	 *         description="签名",
	 *         in="formData",
	 *         name="session_key",
	 *         required=true,
	 *         type="string",
	 *     )
	 * )
	 */
	public function confirmStock()
	{
		$post = $this->post;
		$shop_id = $this->shop_id;
		$restockBn = isset($post['restockBn']) ? $post['restockBn'] : '';
		$this->requireRes($restockBn, '');

		$restock = $this->db->query("select * from app_restock where restock_bn = '$restockBn' and shop_id = '$shop_id' and status = 0")->row();
		$this->requireRes($restock, '未根据货单号找到补货信息,请核对该补货是否完成');
		//更新补货状态
		$this->db->update('app_restock', array('status' => 1, 'updated_at' => date('Y-m-d H:i:s', time()), 'type' => 1), "restock_bn = '$restockBn'");
		//获取补货的详细信息
		$restockId = $restock->id;
		$restockDetails = $this->db->query("select * from app_restock_detail where restock_id = '$restockId'")->result_array();

		foreach ($restockDetails as $v) {
			//判断是否存在
			if ($this->db->query("select * from app_shop_goods where shop_id = '$shop_id' and goods_id = '{$v['goods_id']}' ")->row_array()) {
				$this->db->query("update app_shop_goods set num = num +'{$v['num']}' ,restock_id = '$restockId' where shop_id = '$shop_id' and goods_id = '{$v['goods_id']}' ");
			} else //没有则插入一条
			{
				$this->db->query("insert into app_shop_goods(shop_id,goods_id,restock_id,num) VALUES ('$shop_id', '{$v['goods_id']}','$restockId','{$v['num']}') ");
			}
		}

		$ret['code'] = 2000;
		$ret['msg'] = '收货成功';
		$ret['data'] = '';
		send_json($ret);
	}

	//根据商户id获取商户信息

	/**
	 * @SWG\Post(
	 *     summary="获取商户信息",
	 *     path="/shop/getShopInfo",
	 *     tags={"商户端"},
	 *     operationId="getShopInfo",
	 *     produces={"application/json"},
	 *     @SWG\Response(
	 *         response=200,
	 *         description="成功",
	 *     ),
	 *     @SWG\Response(
	 *         response="401",
	 *         description="参数不全",
	 *     ),
	 *     @SWG\Parameter(
	 *         description="签名",
	 *         in="formData",
	 *         name="session_key",
	 *         required=true,
	 *         type="string",
	 *     )
	 *  )
	 */
	public function getShopInfo()
	{

		$post = $this->post;
		$shop_id = $this->shop_id;
		$shop_info = $this->db->query("select * from `app_shop` where `id` = '$shop_id'")->row_array();
		$shop_info['shop_pic'] = isset($shop_info['pic']) ? $shop_info['pic'] : '';
		$res = $this->db->query("select * from app_key_value where 1")->result_array();
		foreach ($res as $v) {
			$shop_info[$v['k']] = $v['v'];
		}
		send_json(
			array('code' => 2000, 'msg' => 'ok', 'data' => $shop_info)
		);
	}

	//返回参数失败信息
	private function requireRes($s, $r)
	{

		if ($r) {
			if (!$s) {
				send_json(
					array('code' => 4004, 'msg' => $r, 'data' => '')
				);
			}

		} else {
			if (is_array($s)) {
				foreach ($s as $v) {
					$this->requireRes($v, '');
				}
			} else {
				if (!$s) {
					send_json(
						array('code' => 4000, 'msg' => '参数错误', 'data' => '')
					);
				}

			}
		}
	}

	public function onlineOrderDetail()
	{

		$post = $this->post;
		$shop_id = $this->shop_id;
		if (!$shop_id) {
			send_json(
				array('code' => 4000, 'msg' => '参数错误', 'data' => '')
			);
		}
		$orderId = isset($post['orderId']) ? trim($post['orderId']) : '';
		$this->requireRes($orderId, '');

		$this->requireRes($this->db->query("select * from app_order where id = '$orderId'")->row_array(), '未根据订单ID找到订单');
		//订单信息
		$data = $this->db->query("
              select
                a.order_bn , a.freight,a.tracking_number,a.delivery_method,a.delivery_time,a.status,a.created_at,a.payment_at,a.delivered_at,a.completed_at,a.refunded_at,a.closed_at,b.status as online_status
              from
                `app_order` a
			  join
				app_order_online b
			  on
				a.id = b.order_id and b.shop_id = $shop_id
              where
                a.`id` = '$orderId' 
             ")->row_array();

		//订单详细信息
		$data['detail'] = $this->db->query("
                          select
                            b.num,c.price, c.pic_4 as pic, c.title,sum(b.num*c.price) as total
                          from
                            app_order_online a
                          left join
                            app_order_online_detail b
						  on
                            a.id = b.online_id
                          left join
                            app_goods c
						  on
                            c.id = b.goods_id
                          where
                            a.order_id = '$orderId'
                            and a.shop_id = $shop_id
						  group by b.id
                          order by
                            a.id desc
                        ")->result_array();

		$data['money'] = 0;
		if (count($data['detail'])) {
			foreach ($data['detail'] as $v) {
				$data['money'] += $v['total'];
			}
		}
		$ret['code'] = 2000;
		$ret['msg'] = '';
		$ret['data'] = $data;
		send_json($ret);
	}

	//打包
	function park()
	{
		$post = $this->post;
		$orderId = isset($post['orderId']) ? trim($post['orderId']) : '';
		$this->requireRes($orderId, '');
		$shop_id = $this->shop_id;
		if (!$shop_id) {
			send_json(
				array('code' => 4000, 'msg' => '参数错误', 'data' => '')
			);
		}
		if ($this->db->query("update app_order_online set status = 1 where order_id = $orderId and shop_id = $shop_id ")) {
			send_json(
				array('code' => 2000, 'msg' => 'ok', 'data' => '')
			);
		} else {
			send_json(
				array('code' => 4004, 'msg' => '操作失败', 'data' => '')
			);
		}
	}


	//联系电话
	function phone(){
		$data=$this->order_model->getServerPhone();
		send_json(
			array('code' => 2000, 'msg' => 'ok', 'data' => $data)
		);
	}

}
